package me.zhengjie.modules.web.rest.chat;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.PageData;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.chat.*;
import me.zhengjie.modules.web.service.chat.IBaiduChatService;
import me.zhengjie.request.ATMRContext;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("web/chat")
@Api(tags = "前端接口-ai助手")
public class BaiduChatController {

    @Autowired
    private IBaiduChatService baiduChatService;

    @PostMapping("history")
    @ApiOperation("前端接口-历史记录")
    public ResponseBaseEntity history(@RequestBody BaiduChatHistoryRequest request){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            PageData<BaiduChatHistoryResponse> list = baiduChatService.history(request);
            response.setBody(list);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("replay")
    @ApiOperation("前端接口-对话")
    public ResponseBaseEntity replay(@RequestBody BaiduChatRequest request){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            BaiduChatResponse detail = baiduChatService.replay(request);
            response.setBody(detail);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @GetMapping("/surplus/times")
    @ApiOperation("前端接口-对话次数查询")
    public ResponseBaseEntity validate(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            String openId = ATMRContext.getContext().getOpenId();
            if (StringUtils.isEmpty(openId)){
                throw new WeChatException("缺少参数");
            }
            BaiduChatResponse detail = new BaiduChatResponse();
            baiduChatService.getSurplusTimes(openId,detail);
            response.setBody(detail);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @GetMapping("/paidList")
    @ApiOperation("前端接口-对话付费列表")
    public ResponseBaseEntity paidList(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            String openId = ATMRContext.getContext().getOpenId();
            if (StringUtils.isEmpty(openId)){
                throw new WeChatException("缺少参数");
            }
            List<BaiduChatPaidListResponse> paidList = baiduChatService.getPaidList();
            response.setBody(paidList);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @GetMapping("/quickReplyList")
    @ApiOperation("前端接口-快捷回复列表")
    public ResponseBaseEntity quickReplyList(){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            String openId = ATMRContext.getContext().getOpenId();
            if (StringUtils.isEmpty(openId)){
                throw new WeChatException("缺少参数");
            }
            List<BaiduChatQuickReplyResponse> quickReplyList = baiduChatService.getQuickReplyList();
            response.setBody(quickReplyList);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}
